#include <string>
#include <vector>
#include <iostream>
#include <thread>
#include "coroutine.hpp"

void run_in_coroutine2() {
    LOG_CO("run_in_coroutine2 begin");
    LOG_CO("run_in_coroutine2 end");
}

void run_in_coroutine() {
    LOG_CO("run_in_coroutine begin");

    LOG_CO("before run_in_coroutine yield");
    jizhe::Coroutine::GetThis()->yield();
    LOG_CO("after run_in_coroutine yield");

    LOG_CO("run_in_coroutine end");
    // coroutine结束之后会自动返回主协程运行
}

void test_coroutine() {
    jizhe::Coroutine::GetThis();
    jizhe::Coroutine::ptr coroutine(new jizhe::Coroutine(run_in_coroutine, 0));
    LOG_CO("before test_coroutine resume");
    coroutine->resume();
    LOG_CO("after test_coroutine resume");
    LOG_CO("before test_coroutine resume again");
    coroutine->resume();
    LOG_CO("after test_coroutine resume again");
}

void test_coroutine2() {
    jizhe::Coroutine::GetThis();
    jizhe::Coroutine::ptr coroutine(new jizhe::Coroutine(run_in_coroutine2, 0));

    coroutine->resume();

    LOG_CO("test_coroutine finish");
}

int main(int argc, char *argv[]) {

    LOG("main begin");

    // test_coroutine2();
    std::vector<std::thread*> pools;

    for(int i=0; i<3; ++i) {
        pools.emplace_back(new std::thread(test_coroutine2));
    }

    for(int i=0; i<3; ++i) {
        pools[i]->join();
    }

    LOG("main end");
}